// pages/carrierUser/carrierMe/index.js
const {
  $Message
} = require('../../dist/base/index');
var api = require('../../utils/api.js')
const user = require('../index/model.js')
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    historyArr:[],
    leave_message:"",
    targetUser:{},
    leaveShow:false,
    appr_show:false,
    targetRecord:{},
    leave_actions:[{
      name: '确定',
      color: '#2d8cf0'
    }],
    appr_actions:[{
      name: '通过',
      color: '#2d8cf0'
    },{
      name: '不通过',
      color: '#FF4500'
    },{
      name: '取消',
      color: '#949494'
    }],
    is_admin:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo:user.user_info,
      is_admin:options.is_admin
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      spinShow:true
    })
    if(this.data.is_admin == 'true'){
      console.log("管理员")
      this.getHistory(0,[],{
        is_approval:0
      })
    }else{
      this.getHistory(0,[],{
        user_info_id:this.data.userInfo._id
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  click_func: function (detail) {
    console.log(detail.currentTarget.id)
    var self = this
    if(this.data.is_admin == 'true'){
      this.setData({
        spinShow:true
      })
      var targetRecord = this.data.historyArr[detail.currentTarget.id]
      //读取用户信息
      api.db.collection('user_info').doc(targetRecord.user_info_id).get().then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        self.setData({
          leave_message:this.data.historyArr[detail.currentTarget.id].feedback,
          appr_show:true,
          targetUser:res.data,
          spinShow:false,
          targetRecord:targetRecord
        })
      })
      return
    }
    this.setData({
      leave_message:this.data.historyArr[detail.currentTarget.id].feedback,
      leaveShow:true
    })
  },
  hide_message(){
    console.log("click")
    this.setData({
      leaveShow:false
    })
  },
  add_leave(){
    wx.navigateTo({
      url: '/pages/createLeave/index',
    })
  },
  getHistory(count, historyArr,whereDic) {
    var self = this
    var func = null
    if (count == 0) {
      func = api.db.collection('user_leave')
        .limit(20).where(whereDic).orderBy('timestamp', 'desc')
    } else {
      func = api.db.collection('user_leave')
        .skip(count)
        .limit(20).where(whereDic).orderBy('timestamp', 'desc')
    }
    //获取单位信息
    func.get({
      success: function (res) {
        // res.data 包含该记录的数据
        console.log(res.data)
        if (res.data.length != 0) {
          historyArr = historyArr.concat(res.data)
          console.log(historyArr)
          count += 20
          self.getHistory(count, historyArr,whereDic)
        } else {
          console.log("结束")
          historyArr.forEach(element => {
            element.timestamp = util.formatTime(new Date(element.timestamp))
          });
          self.setData({
            historyArr: historyArr,
            spinShow:false
          })
        }
      }
    })
  },
  hide_appr(targetIndex){
    console.log(targetIndex.detail.index)
    switch (targetIndex.detail.index) {
    case 0:
      this.changeStatus(1)
      break
    case 1:
      this.changeStatus(2)
      break
    default:
      break
    }
  },
  changeStatus(is_approval){
    var self = this
    this.setData({
      spinShow:true
    })
    console.log(this.data.targetRecord)
    console.log(is_approval)
    wx.cloud.callFunction({
      name:'updateFunc',
      data:{
        elem:{
          // 表示将 done 字段置为 true
          is_approval: is_approval
        },
        db_name:'user_leave',
        db_id:this.data.targetRecord._id
      },success:function(res){
        console.log(res)
        self.setData({
          appr_show:false
        })
        $Message({
          content: '审批提交成功',
          type: 'success'
        });
        self.getHistory(0,[],{
          is_approval:0
        })
      },fail:function(res){
        console.log(res)
      }
    })
    // api.db.collection('user_leave').doc(this.data.targetRecord._id).update({
    //   // data 传入需要局部更新的数据
    //   data: {
    //     // 表示将 done 字段置为 true
    //     is_approval: is_approval
    //   },
    //   success(res) {
        
    //   }
    // })
  }
})